package com.boot.es.service;

import com.boot.es.entity.TestIndexDto;
import com.boot.es.entity.TestIndexEntity;
import com.boot.es.mapper.TestRepository;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Iterator;

@Service
public class TestServiceImpl  implements  TestService{

    @Autowired
    private TestRepository testRepository;


    public TestIndexEntity add(TestIndexEntity entity) {

       return   testRepository.save(entity);

    }

    public void testQuery(TestIndexDto dto) {

        Iterable<TestIndexEntity> indexEntities = null;
        BoolQueryBuilder boolQueryBuilder =  null;
        QueryBuilder queryBuilder = null;
        Iterator iterator = null;


        System.out.println(String.format("查询id=%s的数据",dto.getId()));
        // 查询单个值
        queryBuilder  =   QueryBuilders.termQuery("id",dto.getId().intValue());
        boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(queryBuilder);
        indexEntities =  testRepository.search(boolQueryBuilder);
        iterator = indexEntities.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());  ;
        }


        System.out.println(String.format("查询id in %s的数据",StringUtils.join(dto.getId_in(),",")));
        //查询多个值 in
        queryBuilder = QueryBuilders.termsQuery("id",dto.getId_in());
        boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(queryBuilder);
        indexEntities =  testRepository.search(boolQueryBuilder);
        iterator = indexEntities.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());  ;
        }

      // 查询年龄
        System.out.println(String.format("查询age<= %s的数据",dto.getAge()));
        queryBuilder = QueryBuilders.rangeQuery("age").lte(dto.getAge());
        boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(queryBuilder);
        indexEntities =  testRepository.search(boolQueryBuilder);
        iterator = indexEntities.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());  ;
        }

        System.out.println(String.format("查询name like %s的数据",dto.getName()));
        //范围查询，文本型的，在插入数据时，字段自动添加了 .keyword
        queryBuilder = QueryBuilders.wildcardQuery( "name","*" +  dto.getName() + "*");
        boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(queryBuilder);
        indexEntities =  testRepository.search(boolQueryBuilder);
        iterator = indexEntities.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());  ;
        }


        //queryBuilder = QueryBuilders.multiMatchQuery( );


    }

}
